﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace FirmaTransportowa
{
    public partial class AdminPanelWorkerView : Form
    {

        public AdminPanelWorkerView()
        {
            SqlConnection dbConnection2 = SqlConnectionFactory.getConnection();
            InitializeComponent();
        }

        private void displayWorkers()
        {
            workersDataGridView.DataSource = Worker.getWorkers();
            workersDataGridView.Columns[0].HeaderText = "ID pracownika";
            workersDataGridView.Columns[1].HeaderText = "imię";
            workersDataGridView.Columns[2].HeaderText = "nazwisko";
            workersDataGridView.Columns[3].HeaderText = "data urodzenia";
            workersDataGridView.Columns[4].HeaderText = "pesel";
            workersDataGridView.Columns[5].Visible = false;
        }

        private void AdminPanelWorkerView_Load(object sender, EventArgs e)
        {
            displayWorkers();
        }

        private void changeToUserViewButton_Click(object sender, EventArgs e)
        {
            this.Close();
            AdminPanelUserView adminPanel = new AdminPanelUserView();
            adminPanel.Show();
        }

        private void addWorkerButton_Click(object sender, EventArgs e)
        {
            AdminAddNewWorker adminAddNewWorker = new AdminAddNewWorker();
            adminAddNewWorker.Closed += OnAddNewWorkedClosed;

            adminAddNewWorker.Show();
        }

        private void deleteWorkerButton_Click(object sender, EventArgs e)
        {
            Worker worker = Worker.getById(int.Parse(workersDataGridView.Rows[workersDataGridView.CurrentRow.Index].Cells[0].Value.ToString()));
            worker.active = false;
            worker.save();
            displayWorkers();
            MessageBox.Show("Skasowano pracownika");

        }

        private void adminPanelUserViewMenuItem_Click_1(object sender, EventArgs e)
        {
            this.Close();
            AdminPanelUserView adminPanel = new AdminPanelUserView();
            adminPanel.Show();
        }

        private void adminPanelCarViewMenuItem_Click_1(object sender, EventArgs e)
        {
            this.Close();
            AdminPanelCarView adminPanelCarView = new AdminPanelCarView();
            adminPanelCarView.Show();
        }

        private void refreshButton_Click(object sender, EventArgs e)
        {
            displayWorkers();
        }

        private void editButton_Click(object sender, EventArgs e)
        {
            AdminAddNewWorker adminAddNewWorker = new AdminAddNewWorker(Worker.getById(int.Parse(workersDataGridView.Rows[workersDataGridView.CurrentRow.Index].Cells[0].Value.ToString())), true);
            adminAddNewWorker.Show();
        }

        private void OnAddNewWorkedClosed()
        {
            workersDataGridView.DataSource = null;
            displayWorkers();
        }

        private void AdminPanelWorkerView_HelpButtonClicked(object sender, CancelEventArgs e)
        {
            HelpUtils.showHelp(this, 14);
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.F1)
            {
                HelpUtils.showHelp(this, 14);
                return true;
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }
    }
}